#include "Fight.hh"
#include "../Utils/include/Common.hh"
#include <iostream>
#include <cstdlib>
#include <thread>
#include <chrono>

using namespace std;

// 开始战斗,传入玩家指针和敌人对象
void Fight::startFight(Player* player, Enemy enemy) {
    Utility::clearScreen();
    cout << "========== Fight Begins ==========" << endl;
    cout << "You encountered an enemy: " << enemy.getName() << endl;

    // 战斗主循环,当玩家和敌人都活着时继续
    while (player->isAlive() && enemy.isAlive()) {
        // 显示当前状态
        cout << "-------- Current Status --------" << endl;
        player->display();
        enemy.display();
        cout << "-------------------------" << endl;

        // 显示可选操作
        cout << "Please select an action:" << endl;
        cout << "1. Normal Attack" << endl;
        cout << "2. View Inventory" << endl;
        cout << "3. Use Item" << endl;

        string choice;
        cin >> choice;

        if (choice == "1") {
            // 玩家攻击阶段
            int playerAttackRoll = rand() % 100;
            if (playerAttackRoll < enemy.getSpeed()) {
                cout << "Enemy dodged your attack!" << endl;
                std::this_thread::sleep_for(std::chrono::seconds(1));
            } else {
                cout << "You hit the enemy!" << endl;
                player->attack(enemy);
                std::this_thread::sleep_for(std::chrono::seconds(1));
            }

            // 检查敌人是否被击败
            if (!enemy.isAlive()) {
                cout << "Enemy has been defeated!" << endl;
                break;
            }

            // 敌人攻击阶段
            int enemyAttackRoll = rand() % 100;
            if (enemyAttackRoll < player->getSpeed()) {
                cout << "You dodged the enemy's attack!" << endl;
                std::this_thread::sleep_for(std::chrono::seconds(1));
            } else {
                cout << "Enemy hit you!" << endl;
                enemy.attack(*player);
                std::this_thread::sleep_for(std::chrono::seconds(1));
            }

            // 检查玩家是否被击败
            if (!player->isAlive()) {
                cout << "You have been defeated..." << endl;
                break;
            }
            Utility::clearScreen();
        }
        else if (choice == "2") {
            // 查看背包
            player->displayInventory();
        }
        else if (choice == "3") {
            // 使用物品
            player->displayInventory();
            cout << "Enter the ID of the item to use: ";
            int id;
            cin >> id;

            // 查找并使用物品
            Item* item = player->getInventory().findItem(id);
            if (item) {
                if (item->getEffect() == "heal") {
                    player->heal(item->getValue());
                    cout << "Used " << item->getName() << ", healed " << item->getValue() << " HP." << endl;
                }
                player->getInventory().removeItem(id);
            }
            else {
                cout << "No such item!" << endl;
            }
        }
        else {
            cout << "Invalid input, please re-select." << endl;
        }
    }

    // 战斗结束
    cout << "========== Fight Ends ==========" << endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));
    Utility::pause();
}